package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleClient;
import com.heima.user.feign.MediaClient;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-24
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Override
    public ResponseResult listByStatus(AuthDto dto) {
        // 用户认证列表分页查询
        // 判断传来的status是否为空,为空查询所有,不为空添加查询条件
        //构建查询条件
        LambdaQueryWrapper<ApUserRealname> query = new LambdaQueryWrapper<>();
        // 添加分页条件
        IPage<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        IPage<ApUserRealname> page2 = new Page<>(dto.getPage(), dto.getSize());
        IPage<ApUserRealname> iPage = this.page(page, query);
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), iPage.getTotal(), iPage.getRecords());
        return result;
    }

    public ResponseResult listByStatus2(AuthDto dto) {
        // 用户认证列表分页查询
        // 判断传来的status是否为空,为空查询所有,不为空添加查询条件
        //构建查询条件
        LambdaQueryWrapper<ApUserRealname> query = new LambdaQueryWrapper<>();
        if (dto.getStatus() != null) {
            query.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        // 添加分页条件
        IPage<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        IPage<ApUserRealname> iPage = this.page(page, query);
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), iPage.getTotal(), iPage.getRecords());
        return result;
    }


    @Autowired
    private IApUserService userService;

    @Override
    public ResponseResult auth(AuthDto dto, int i) {
        // 根据id查询认证表
        ApUserRealname apUserRealname = this.getById(dto.getId());
        // 审核不通过
        if (i == 0) {
            // 拒绝,修改认证表状态为2,并记录原因
            apUserRealname.setStatus(2);
            apUserRealname.setReason(dto.getReason());
            //更新
            this.updateById(apUserRealname);
            return ResponseResult.okResult();
        }
        // 审核成功
        // 查询用户认证表信息
        ApUser apUser = userService.getById(apUserRealname.getUserId());
        // 创建自媒体账号
        WmUser wmUser = createWmUser(apUser);
        if (wmUser != null) {
            // 创建文章作者账号
            ApAuthor apAuthor = createApAuthor(apUser, wmUser);
            if (apAuthor != null) {
                // 修改用户认证表状态为9
                apUserRealname.setStatus(9);
                this.updateById(apUserRealname);
                // 修改用户注册表的flag为1 自媒体人
                apUser.setFlag(1);
                userService.updateById(apUser);
            }
        }
        return ResponseResult.okResult();
    }


    @Autowired
    private MediaClient mediaClient;

    @Autowired
    private ArticleClient articleClient;

    /**
     * 保存作者
     *
     * @param apUser
     * @param wmUser
     * @return
     */
    private ApAuthor createApAuthor(ApUser apUser, WmUser wmUser) {
        ApAuthor author = new ApAuthor();
        author.setName(apUser.getName());
        author.setUserId(apUser.getId());
        author.setWmUserId(wmUser.getId());
        author.setType(2);
        ResponseResult<ApAuthor> apAuthorResponseResult = articleClient.saveApAuthor(author);
        if (apAuthorResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            ApAuthor apAuthor = apAuthorResponseResult.getData();
            if (apAuthor != null) {
                wmUser.setApAuthorId(apAuthor.getId());
                // 更新自媒体账号
                mediaClient.updateWmUser(wmUser);
            }
            return apAuthor;
        }
        return null;
    }


    /**
     * 保存作者
     *
     * @param apUser
     * @param wmUser
     * @return
     */
    private ApAuthor createApAuthor2(ApUser apUser, WmUser wmUser) {
        ApAuthor author = new ApAuthor();
        author.setName(apUser.getName());
        author.setUserId(apUser.getId());
        author.setWmUserId(wmUser.getId());
        author.setType(2);
        ResponseResult<ApAuthor> apAuthorResponseResult = articleClient.saveApAuthor(author);
        if (apAuthorResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            ApAuthor apAuthor = apAuthorResponseResult.getData();
            if (apAuthor != null) {
                wmUser.setApAuthorId(apAuthor.getId());
                // 更新自媒体账号
                mediaClient.updateWmUser(wmUser);
            }
            return apAuthor;
        }
        return null;
    }

    /**
     * 创建自媒体账号
     *
     * @param apUser
     * @return
     */
    private WmUser createWmUser(ApUser apUser) {
        WmUser user = new WmUser();
        // 基本信息从apUser复制
        BeanUtils.copyProperties(apUser, user);
        user.setId(null);
        user.setApUserId(apUser.getId());
        user.setNickname(apUser.getName());
        user.setStatus(9);
        user.setType(0);
        // 发起远程调用
        ResponseResult<WmUser> wmUserResponseResult = mediaClient.saveWmUser(user);
        if (wmUserResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            WmUser wmUser = wmUserResponseResult.getData();
            return wmUser;
        }
        return null;
    }
}
